﻿<template>
  <a-card :bordered="false" :style="{height: getCardHeight,overflow: 'auto'}">
    <div class="table-operator">
      <a-button v-if="hasPerm('Elevator_Series.Add')" type="primary" icon="plus" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('Elevator_Series.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
      >删除</a-button>
      <a-button type="primary" icon="redo" @click="handleRefreshPage()">刷新</a-button>
    </div>
    <a-row :gutter="20">
      <a-col span="4">
        <a-directory-tree
          v-if="isShowTree"
          :blockNode="true"
          :load-data="onLoadData"
          :tree-data="treeData"
          defaultExpandAll
          expandAction="dblclick"
          @select="onSelect"
          :style="{ height: getTreeListHeight,overflow: 'auto' }"
        />
      </a-col>
      <a-col span="20">
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="10">
              <a-col :md="4" :sm="24">
                <a-form-item label="查询类别">
                  <a-select v-model="queryParam[0].condition">
                    <!--<a-select-option key="Code">自定义编码</a-select-option>-->
                    <a-select-option key="Name">系列名称</a-select-option>
                    <!--<a-select-option key="Remark">备注</a-select-option>-->
                    <a-select-option key="BrandName">品牌名称</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item>
                  <a-input allowClear @keydown="inputKeyDown" v-model="queryParam[0].keyword" placeholder="关键字" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <a-table
          ref="table"
          :scroll="{ y: tableHight }"
          :columns="columns"
          :rowKey="(row, index) => row.Id"
          :dataSource="data"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'checkbox' }"
          :bordered="true"
          size="small">
          <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime,'YYYY-MM-DD HH:mm') }}</span>
          <span slot="action" slot-scope="text, record">
            <template>
              <a v-if="hasPerm('Elevator_Series.Edit')" @click="handleEdit(record.Id)">编辑</a>
              <a-divider v-if="hasPerm('Elevator_Series.Edit')&&hasPerm('Elevator_Series.Delete')" type="vertical" />
              <a v-if="hasPerm('Elevator_Series.Delete')" @click="handleDelete([record.Id])">删除</a>
            </template>
          </span>
        </a-table>
      </a-col>
    </a-row>
    <edit-form ref="editForm" :parentObj="this" @returnMain="handleReturnMain"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
const columns = [
  { title: '排序', dataIndex: 'Sort', align: 'center', width: '5%' },
  { title: '名称', dataIndex: 'Name', align: 'left', width: '20%' },
  { title: '编码', dataIndex: 'Code', align: 'left', width: '15%' },
  { title: '所属品牌', dataIndex: 'BrandName', align: 'center', width: '15%' },
  { title: '创建时间', dataIndex: 'CreateTime', width: '10%', align: 'center', scopedSlots: { customRender: 'CreateTime' } },
  { title: '备注', dataIndex: 'Remark', align: 'center', width: '20%' },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm
  },
  computed: {
    // 文件区高度
    getCardHeight: function () {
      return (this.tableHeight + 250) + 'px' // 自定义高度需求
    },
    getTreeListHeight: function () {
      return (this.tableHeight + 150) + 'px' // 树形高度
    }
  },
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHight = this.getTableHeight(true) + 24.5
    })
    this.getDataList()
  },
  data () {
    return {
      tableHight: 570, // 表格高度
      data: [],
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Sort', order: 'asc' },
      loading: false,
      columns,
      queryParam: [{ condition: 'Name', keyword: '' }, { condition: 'BrandId', keyword: '' }],
      selectedRows: [],
      selectedRowKeys: [],
      treeData: [
        { title: '全部', key: '0' }
      ],
      isShowTree: true
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Elevator_Manage/Elevator_Series/GetDataListByMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order || 'desc',
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    inputKeyDown (e) {
      if (e.keyCode === 13) {
        if (!this.queryParam[0].keyword) return this.$message.error('请输入查询条件')
        this.getDataList()
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    // 树形菜单选择
    onSelect (selectedKeys, { node }) {
      this.GetTreeNodeListInfo(node.eventKey)
    },
    // 加载树形数据
    onLoadData (treeNode) {
      const _this = this
      return new Promise(resolve => {
        if (treeNode.dataRef.children) {
          resolve()
          return
        }
        setTimeout(() => {
          this.$http.post('/Elevator_Manage/Elevator_Brand/GetElevatorBrandTreeNodeListInfo').then(resJson => {
            if (resJson.Success) {
              if (resJson.Data && resJson.Data.length > 0) {
                treeNode.dataRef.children = resJson.Data
                _this.treeData = [..._this.treeData]
              }
              _this.GetTreeNodeListInfo(treeNode.eventKey)
            } else {
              _this.$message.error(resJson.Msg)
            }
          })
          resolve()
        }, 100)
      })
    },
    // 根据传入参数获取树形节点信息
    GetTreeNodeListInfo (brandId) {
      if (brandId == '0') {
        this.queryParam[1].keyword = ''
      } else {
        this.queryParam[1].keyword = brandId
      }
      this.getDataList()
    },
    handleRefreshPage () {
      this.pagination.current = 1
      this.pagination.pageSize = 25
      this.queryParam = [{ condition: 'Name', keyword: '' }, { condition: 'BrandId', keyword: '' }]
      this.treeData = [{ title: '全部', key: '0' }]
      this.isShowTree = false
      setTimeout(() => { this.isShowTree = true }, 10)
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建品牌下属系列表信息')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑品牌下属系列表信息')
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/Elevator_Manage/Elevator_Series/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    handleReturnMain () {
      this.getDataList()
    },
    // 表格点击选中取消
    loadRow (record, index) {
      return {
        on: {
          click: () => {
            // 记录为disabled则直接返回，默认为不可选
            if (record.disabled) return
            const type = 'checkbox'
            const key = record.Id
            let selectedRows = this.selectedRows
            let selectedRowKeys = this.selectedRowKeys
            if (type === 'radio') {
              selectedRowKeys = [key]
              selectedRows = [record]
            }
            if (!this.selectedRowKeys.includes(key)) {
              selectedRowKeys.push(key)
              selectedRows.push(record)
            } else {
              const index = this.selectedRowKeys.findIndex(skey => skey === key)
              selectedRows.splice(index, 1)
              selectedRowKeys.splice(index, 1)
            }
            this.updateSelect(selectedRowKeys, selectedRows)
            this.onSelectChange(selectedRowKeys, selectedRows)
          }
        }
      }
    },
    // 更新选中操作
    updateSelect (selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    }
  }
}
</script>
